package com.ellricky.baselib.ui.student

import com.andr.common.ui.activity.BaseToolBarActivity
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.ResUtil
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.R
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.new.CourseType
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEListCallback
import kotlinx.android.synthetic.main.tc_activity_student_class_record.*
import java.util.*

class StudentClassRecordActivity : BaseToolBarActivity<Void, BasePresenter<Void>>() {

    private val memberId: String by lazy {
        intent.getStringExtra(BaseConstant.KEY_ID)
    }

    override fun getContentLayout(): Int {
        return R.layout.tc_activity_student_class_record
    }

    override fun initUIView() {
        super.initUIView()
        mTvTitle.text = ResUtil.getStr(this, R.string.base_class_record)

        loadClassType()
    }

    private fun loadClassType() {
        val url = EAppUtil.getApiHost()
        val paramMap: MutableMap<String, String> = HashMap()
        paramMap["action"] = "teacherCourseTypeList"
        paramMap["eid"] = LocalCacheUtil.getUid()

        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEListCallback<CourseType>(this) {
                override fun onSuccess(data: MutableList<CourseType>, msg: String?) {
                    if (data.isNotEmpty()) {
                        val courseNameList = data.map { it.course_name }
                        val list = arrayListOf<StudentClassRecordFragment>()
                        data.forEach {
                            list.add(
                                StudentClassRecordFragment.newInstance(
                                    it.course_id,
                                    memberId,
                                    it.course_name
                                )
                            )
                        }
                        tabLayout.setUp(supportFragmentManager, list.toList(), courseNameList, vp)
                    }
                }
            })
    }
}